﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using DATN.DataAccess;
using DATN.Entities;

namespace DATN.Business
{
    public class UserBusiness
    {
        public int GetRole(string account, string password)
        {
            if (string.IsNullOrEmpty(account)) return -1;
            if (string.IsNullOrEmpty(password)) return -1;
            UserDataAccess userDao = new UserDataAccess();
            try
            {
                User user = userDao.GetUser(account, password);
                return user != null ? user.Role : -1;
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return -1;
        }

        public Collection<User> GetAllUser()
        {
            UserDataAccess userDao = new UserDataAccess();
            return userDao.GetAllUser();
        }
        //Kiểm tra tính hợp lệ Email
        public bool ValidateEmail(string email)
        {
            if (string.IsNullOrEmpty(email)) return false;
            var userDao = new UserDataAccess();
            try
            {
                return userDao.CheckMail(email) > 0;
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return false;
        }

        //Update Password theo mail
        public bool UpdatePasswordByEmail(string email, string password)
        {
            if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password)) return false;
            var userDao = new UserDataAccess();
            try
            {
                return userDao.UpdatePasswordByEmail(email, password);
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return false;
        }

        //Update Password theo user
        public bool UpdatePasswordByUser(string user, string password)
        {
            if (string.IsNullOrEmpty(user) || string.IsNullOrEmpty(password)) return false;
            var userDao = new UserDataAccess();
            try
            {
                return userDao.UpdatePasswordByUser(user, password);
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return false;
        }

        //Thêm người dùng
        public bool InsertUser(string userName, string password,string email, int role, string maNV)
        {
            if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password)) return false;
            var userDao = new UserDataAccess();
            try
            {
                return userDao.InsertUser(userName, password,email,role,maNV);
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return false;
        }
    }
}
